package com.dxd.onlineshop.controller;

import com.dxd.onlineshop.pojo.UserFeedback;
import com.dxd.onlineshop.service.UserFeedbackService;
import com.dxd.onlineshop.utils.JwtUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/api/feedback")
public class UserFeedbackController {

    private final UserFeedbackService userFeedbackService;

    @Autowired
    public UserFeedbackController(UserFeedbackService userFeedbackService) {
        this.userFeedbackService = userFeedbackService;
    }

    //创建反馈
    @PostMapping
    public ResponseEntity<UserFeedback> createFeedback(@RequestBody UserFeedback feedback, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        if (token != null && token.startsWith("Bearer ")) {
            token = token.substring(7); // 移除 "Bearer " 前缀
        }

        try {
            Map<String, Object> claims = JwtUtil.parseToken(token);
            Integer userId = (Integer) claims.get("userId");

            if (userId == null) {
                return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
            }

            // 设置用户ID到反馈对象
            feedback.setUserId(userId);

            UserFeedback createdFeedback = userFeedbackService.createFeedback(feedback);
            return new ResponseEntity<>(createdFeedback, HttpStatus.CREATED);
        } catch (Exception e) {
            // 处理token解析错误
            return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
        }
    }

    //根据ID获取反馈
    @GetMapping("/{id}")
    public ResponseEntity<UserFeedback> getFeedbackById(@PathVariable Long id) {
        UserFeedback feedback = userFeedbackService.getFeedbackById(id);
        if (feedback != null) {
            return ResponseEntity.ok(feedback);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    //获取用户所有反馈
    @GetMapping("/user")
    public ResponseEntity<List<UserFeedback>> getFeedbackByUserId(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        try {
            Map<String,Object> claims = JwtUtil.parseToken(token);
            Integer userId = (Integer) claims.get("userId");
            List<UserFeedback> feedbacks = userFeedbackService.getFeedbackByUserId(userId);
            return ResponseEntity.ok(feedbacks);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResponseEntity.notFound().build();
    }

    //获取所有反馈
    @GetMapping
    public ResponseEntity<List<UserFeedback>> getAllFeedback() {
        List<UserFeedback> feedbacks = userFeedbackService.getAllFeedback();
        return ResponseEntity.ok(feedbacks);
    }

    //获取特定类型反馈
    @GetMapping("/type/{type}")
    public ResponseEntity<List<UserFeedback>> getFeedbackByType(@PathVariable UserFeedback.FeedbackType type) {
        List<UserFeedback> feedbacks = userFeedbackService.getFeedbackByType(type);
        return ResponseEntity.ok(feedbacks);
    }

    //更新反馈
    @PutMapping("/{id}")
    public ResponseEntity<UserFeedback> updateFeedback(@PathVariable Long id, @RequestBody UserFeedback feedback) {
        feedback.setId(id);
        UserFeedback updatedFeedback = userFeedbackService.updateFeedback(feedback);
        if (updatedFeedback != null) {
            return ResponseEntity.ok(updatedFeedback);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    //删除反馈
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteFeedback(@PathVariable Long id) {
        userFeedbackService.deleteFeedback(id);
        return ResponseEntity.noContent().build();
    }

    //搜索反馈
    @GetMapping("/search")
    public ResponseEntity<List<UserFeedback>> searchFeedbackByContent(@RequestParam String keyword, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        if (token != null && token.startsWith("Bearer ")) {
            token = token.substring(7); // 移除 "Bearer " 前缀
        }

        try {
            Map<String, Object> claims = JwtUtil.parseToken(token);
            Integer userId = (Integer) claims.get("userId");

            if (userId == null) {
                return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
            }

            List<UserFeedback> feedbacks = userFeedbackService.searchFeedbackByContentAndUserId(keyword, userId);
            return ResponseEntity.ok(feedbacks);
        } catch (Exception e) {
            // 处理token解析错误
            return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
        }
    }
}